import moment from 'moment';
import { detectionMinTime } from '@containers/asset-manage/components/detection-task-modal/constants';

/**
 * @param {{timing: {}, endAt, startAt}} state
 * @param {moment} state.timing.dailyStart 每日开始时间
 * @param {moment} state.timing.dailyEnd 每日结束时间
 * @param {moment} state.startAt 扫描开始时间
 * @param {moment} state.endAt  扫描结束时间
 * @return {boolean} 是否满足最小扫描时间
 * */
const checkDetectionTime = state => {
  const { timing = {}, startAt = moment(), endAt } = state;
  if (!endAt || !startAt) {
    return true;
  }

  if (endAt - startAt < 29 * 60 * 1000) {
    return false;
  }

  let { dailyStart, dailyEnd } = timing;
  let detectEnoughTime = true;

  // 不跨零点
  if (dailyStart && dailyEnd && dailyEnd > dailyStart) {
    const firstDateStr = startAt.format('YYYY-MM-DD');
    const firstDay = moment(startAt).endOf('day');

    // 第一天
    let firstStart = moment(`${firstDateStr} ${dailyStart.format('HH:mm')}`);
    firstStart = moment.max(firstStart, startAt);
    let firstEnd = moment(`${firstDateStr} ${dailyEnd.format('HH:mm')}`);
    // 单日任务还是多日任务
    firstEnd = endAt < firstDay ? moment.min(firstEnd, endAt) : firstEnd;

    if (firstEnd - firstStart < detectionMinTime) {
      // 第一天扫描时间不够
      let firstDayTime = firstEnd - firstStart;
      firstDayTime = Math.max(firstDayTime, 0);

      if (endAt < firstDay) {
        // 结束日期只到第一天
        return false;
      }

      // 中间
      const periodTime = dailyEnd - dailyStart;
      let runningDay = endAt.diff(startAt, 'days') - 2;
      runningDay = Math.max(runningDay, 0);
      const runningDayTime = runningDay * periodTime;

      // 最后一天
      const lastDateStr = endAt.format('YYYY-MM-DD');
      let lastEnd = moment(`${lastDateStr} ${dailyEnd.format('HH:mm')}`);
      let lastDayTime =
        moment.min(lastEnd, endAt) - moment(`${lastDateStr} ${dailyStart.format('HH:mm')}`);
      lastDayTime = Math.max(lastDayTime, 0);

      const totalTime = firstDayTime + runningDayTime + lastDayTime;

      if (totalTime < detectionMinTime) {
        detectEnoughTime = false;
      }
    }
  }

  // 跨零点
  if (dailyStart && dailyEnd && dailyEnd < dailyStart) {
    const firstDateStr = startAt.format('YYYY-MM-DD');
    const firstDay = moment(startAt).endOf('day');

    // 第一天
    let firstStart = moment(`${firstDateStr} ${dailyStart.format('HH:mm')}`);
    firstStart = moment.max(firstStart, dailyStart);
    let firstEnd = endAt > firstDay ? firstDay : endAt;

    if (firstEnd - firstStart < detectionMinTime) {
      // 第一天扫描时间不够
      let firstDayTime = firstEnd - firstStart;
      firstDayTime = Math.max(firstDayTime, 0);

      if (endAt < firstDay) {
        // 结束日期只到第一天
        return false;
      }

      // 中间
      const periodTime = dailyEnd - dailyStart + 24 * 60 * 60 * 1000;
      let runningDay = endAt.diff(startAt, 'days') - 2;
      runningDay = Math.max(runningDay, 0);
      const runningDayTime = runningDay * periodTime;

      // 最后一天
      const lastDateStr = endAt.format('YYYY-MM-DD');
      const lastDayStart = moment(endAt).startOf('day');
      let lastEnd = moment(`${lastDateStr} ${dailyEnd.format('HH:mm')}`);

      const earlyTime = moment.min(lastEnd, endAt) - lastDayStart;
      let lastDayTime = endAt - moment(`${lastDateStr} ${dailyStart.format('HH:mm')}`);
      lastDayTime = Math.max(lastDayTime, 0) + Math.max(earlyTime, 0);

      const totalTime = firstDayTime + runningDayTime + lastDayTime;
      if (totalTime < detectionMinTime) {
        detectEnoughTime = false;
      }
    }
  }

  return detectEnoughTime;
};

export default checkDetectionTime;
